package com.scut.statistics_website.service;

import com.scut.statistics_website.config.GlobalVariables;
import com.scut.statistics_website.model.Supplier;
import com.scut.statistics_website.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class SupplierService {
    @Autowired
    private RedisUtil redisUtil;

    public ArrayList<String> getCachedSuppliers() {
        ArrayList<String> supplierIds = new ArrayList<>();
        Set<Object> suppliersSet = redisUtil.sGet(GlobalVariables.ALIVE_SUPPLIER_SET_KEY);
        for (Object supplierId : suppliersSet) {
            supplierIds.add((String)supplierId);
        }
        return supplierIds;
    }


    public Supplier getSupplier(String id) {
        Map<Object, Object>  rawSupplier =  redisUtil.hmget(id);
        return new Supplier(id, (float)rawSupplier.get("lon"), (float)rawSupplier.get("lat"));
    }

}
